<template>
  <div>
    <h1>员工列表 欢迎{{ user.name }}</h1>
    <el-button
      type="success"
      @click="handleInsert()">新增员工
    </el-button>
    <template>
      <el-table
        :data="userList.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
        style="width: 100%">
        <el-table-column
          label="ID"
          prop="id"
          width="100">
        </el-table-column>
        <el-table-column
          label="名字"
          prop="name"
          width="100">
        </el-table-column>
        <el-table-column
          label="性别"
          prop="sex"
          width="100">
        </el-table-column>
        <el-table-column
          prop="birthday"
          label="出生日期">
        </el-table-column>
        <el-table-column
          prop="registTime"
          label="注册日期">
        </el-table-column>
        <el-table-column
          align="right">
          <template slot="header" slot-scope="scope">
            <el-input
              v-model="search"
              size="mini"
              placeholder="输入名字搜索"/>
          </template>
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleLook(scope.row)">查看
            </el-button>
            <el-button
              size="mini"
              @click="handleEdit(scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>

    <!--  分页显示  -->
    <div :class="pageClass">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5,10,20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!--  弹框  -->
    <el-dialog
      title="查看用户"
      :visible.sync="dialogVisible"
      width="30%">
      <p>ID: {{ userLook.id }}</p>
      <p>名字: {{ userLook.name }}</p>
      <p>性别: {{ userLook.sex }}</p>
      <p>出生日期: {{ userLook.birthday }}</p>
      <p>注册日期: {{ userLook.registTime }}</p>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Home",
  methods: {
    // 每页显示几条数据
    handleSizeChange(pageSize) {
      // 查询用户列表
      this.getUserList(this.currentPage, pageSize);
    },
    // 切换第几页
    handleCurrentChange(pageIndex) {
      // 查询用户列表
      this.getUserList(pageIndex, this.pageSize);
    },
    handleInsert() {
      // 路由到添加员工页面
      this.$router.push('userAdd');
    },
    // 点击查看用户按钮触发的事件
    handleLook(row) {
      // 显示弹框
      this.userLook = row;
      this.dialogVisible = true;
    },
    handleEdit(row) {
      this.$router.push(`/userEdit/${row.id}`);
    },
    handleDelete(row) {
      let vm = this;
      // 发送删除数据的请求
      this.axios({
        url: `http://localhost:9090/vue/user/${row.id}`,
        method: 'GET'
      }).then(function (res) {
        // 判断是否删除成功
        if (res.data.code == 0) {
          vm.$message.success('删除成功');
          // 查询一下用户列表
          vm.getUserList();
        }
      }).catch(function (err) {
        vm.$message.error('删除失败');
      });
    },
    // 查询用户的方法,pageIndex第一个参数是查看第几页 pageSize第二个参数是每页显示几条数据
    getUserList(pageIndex, pageSize) {
      // 发送axios请求
      let vm = this;
      this.axios({
        url: `http://localhost:9090/vue/users?pageIndex=${pageIndex}&pageSize=${pageSize}`,
        method: 'GET'
      }).then(function (res) {
        // console.log(res.data.data);
        vm.userList = res.data.data.list;
        // 根据后端返回的数据设置分页需要的参数
        vm.total = res.data.data.total;
        vm.pageSize = res.data.data.pageSize;
        vm.currentPage = res.data.data.pageNum;
      });
    }
  },
  data() {
    return {
      user: this.$store.getters.getUser, //从store中取出来数据
      userList: [],
      search: '',
      dialogVisible: false,  // 弹是否可见
      userLook: {
        id: 0,
        name: '',
        sex: '',
        birthday: '',
        registTime: ''
      },
      pageClass: "myPage", //自定义的分页样式
      currentPage: 1, //默认显示的第几页,真实是数据是后端返回的
      pageSize: 10, //每页显示几条数据,真实是数据是后端返回的
      total: 16 // 总共多少条数据,真实是数据是后端返回的
    }

  },
  mounted() {
    // 页面挂载完成后，查询用户
    this.getUserList(this.currentPage, this.pageSize);
  }
}
</script>

<style scoped>
.myPage {
  margin-top: 40px;
  text-align: center;
}
</style>
